define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'code/index' + location.search,
                    add_url: 'code/add',
                    edit_url: 'code/edit',
                    del_url: 'code/del',
                    multi_url: 'code/multi',
                    import_url: 'code/import',
                    table: 'code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'serial_number', title: __('Serial_number'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'code', title: __('Code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'hits', title: __('Hits'),operate:false},
                        {field: 'query_time', title: __('Query_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'query_address', title: __('Query_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'pici.pici_code', title: __('Pici.pici_code'), operate: false},
                        {field: 'pro.name', title: __('Pro.name'), operate: 'LIKE', table: table, class: 'autocontent', searchList: $.getJSON('pro/searchlist'),},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('二维码'),
                            table: table,
                            operate:false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('二维码'),
                                    title: __('二维码'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'code/qrcode/pici_id/{pici_id}',
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ],
                queryParams: function(params){
                    var filter = JSON.parse(params.filter);
                    if(filter.code=='index'){
                        filter.code='';
                    }
                    var op = JSON.parse(params.op);
                    var pici_id = Fast.api.query('ids');
                    if (pici_id != null){
                        filter['pici_id']=Fast.api.query('ids');
                        op['pici_id'] = '=';
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
